/**
 * 课程页面
 */


app.controller('courseCtrl', function ($scope, $location, $rootScope, $http, $ionicPopup, $state, dialogsManager, $ionicHistory) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    //最近课程数据
    var url = 'http://qa.aixingfu.net/v1/api-member/get-member-class?memberId=' + $rootScope.globalInfo.user.ids;
    //下拉刷新
    $scope.doRefresh = function () {
        $http.get(url)
            .success(function (newItems) {
                $scope.recentCourse = newItems;
            })
            .finally(function () {
                $scope.$broadcast('scroll.refreshComplete');
            });
    };
    $scope.latelyCourse = function () {
        $http({
            url: url,
            method: 'GET',
        }).success(function (date) {
            $scope.recentCourse = date;
        }).error(
            function (data) {
                console.log(data);
                $scope.showMessage = function () {
                    dialogsManager.showMessage('系统错误,请联系工作人员');
                }
                $scope.showMessage();
            }
            );
    }
    $scope.latelyCourse();
    //点击订单列表传输数据
    $scope.onlClick1 = function (id, aboutId, type, object, typew) {
        if (typew == true) {
            $scope.showMessage = function () {
                dialogsManager.showMessage('正在上课中');
            }
            $scope.showMessage();
            return
        }
        //获取传过来的id
        $rootScope.idsw1 = id;
        $rootScope.aboutId = aboutId;
        if (type == 1) {
            $rootScope.type1 = "charge";
        }
        if (type == 2) {
            $rootScope.type1 = "group";
        }
        $scope.clickAllOrderObject = object;

        if ($scope.clickAllOrderObject.unusedFlag == true) {
            //跳转待使用详情页面
            $rootScope.id = id;
            $state.go('tab.unusedDetail', id, { location: 'replace' })
        }
        if ($scope.clickAllOrderObject.cancelFlag == true) {
            //跳转取消详情页面
            $rootScope.idCc = id;
            location.href = '#/tab/course/cancelCourseDetail/:' + id;
        }
        if ($scope.clickAllOrderObject.classStatus == false) {
            //跳转完成评价详情页面
            $rootScope.idCcD = id;
            location.href = '#/tab/course/evaluateCompleteDetail/:' + id;
        }
        if ($scope.clickAllOrderObject.unusedFlag == false) {
            //跳转待使用详情页面
            $rootScope.id = id;
            $state.go('tab.unusedDetail', id, { location: 'replace' })
        }
    }
    //跳转待使用列表
    $scope.unused = function () {
        location.href = '#/tab/course/unused';
    }
    //跳转待评价列表
    $scope.futureEvaluate = function () {
        location.href = '#/tab/course/futureEvaluate';
    }
    //跳转已取消列表
    $scope.cancelCourse = function () {
        location.href = '#/tab/course/cancelCourse';
    }
    //点击跳转全部订单页面
    $scope.allOrders = function () {
        location.href = '#/tab/course/allOrders';
    }
    //已完成详情页面
    $scope.evaluateCompleteDetail = function () {
        location.href = '#/tab/course/evaluateCompleteDetail';
    }
});

/**
 * Created by DELL on 2017/4/20.
 * 全部订单页面
 */

app.controller('allOrdersCtrl', function ($scope, $rootScope, $stateParams, $http, dialogsManager, $ionicHistory, $state) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }

    var url = 'http://qa.aixingfu.net/v1/api-member/get-whole-member-class?memberId=' + $rootScope.globalInfo.user.ids;
    //下拉刷新
    $scope.doRefresh = function () {
        $http.get(url)
            .success(function (newItems) {
                $scope.fullCourse = newItems;
            })
            .finally(function () {
                $scope.$broadcast('scroll.refreshComplete');
            });
    };
    $scope.allOrdersData = function () {
        $http({
            url: url,
            method: 'GET',
        }).success(function (date) {
            $scope.fullCourse = date;
        }).error(
            function (data) {
                console.log(data);
                $scope.showMessage = function () {
                    dialogsManager.showMessage('系统错误,请联系工作人员');
                }
                $scope.showMessage();
            }
            );
    }
    $scope.allOrdersData();

    //点击订单列表传输数据
    $scope.onlClick = function (id, aboutId, type, object, typew) {
        if (typew == true) {
            $scope.showMessage = function () {
                dialogsManager.showMessage('正在上课中');
            }
            $scope.showMessage();
            return
        }
        //获取传过来的id
        $rootScope.idsw = id;
        $rootScope.aboutId = aboutId;
                if (type == 1) {
            $rootScope.type1 = "charge";
        }
        if (type == 2) {
            $rootScope.type1 = "group";
        }
        $scope.clickAllOrderObject = object;
        if ($scope.clickAllOrderObject.unusedFlag == true) {
            //跳转待使用详情页面
            $rootScope.id = id;
            $state.go('tab.unusedDetail', id, { location: 'replace' })
        }
        if ($scope.clickAllOrderObject.classStatus == true) {
            //跳转待评价页面
            $rootScope.idFe = id;
            location.href = '#/tab/course/futureEvaluateDetail/:' + id;
        }
        if ($scope.clickAllOrderObject.cancelFlag == true) {
            //跳转取消详情页面
            $rootScope.idCc = id;
            location.href = '#/tab/course/cancelCourseDetail/:' + id;
        }
        if ($scope.clickAllOrderObject.classStatus == false) {
            //跳转完成评价详情页面
            $rootScope.idCcD = id;
            location.href = '#/tab/course/evaluateCompleteDetail/:' + id;
        }
    }
});

/**
 * Created by DELL on 2017/4/20.
 * 课程结束
 */

app.controller('evaluateCompleteDetailCtrl', function ($scope, $rootScope, $stateParams, dialogsManager, $ionicHistory, $http) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    $http({ method: 'get', url: 'http://qa.aixingfu.net/v1/api-member/get-class-comment?aboutId=' + $rootScope.aboutId + '&type=' + $rootScope.type1 }).then(function (data) {
        $scope.evaluateCompleteDetailData = data.data;
    }, function (error) {
        console.log(error);
        $scope.showMessage = function () {
            dialogsManager.showMessage('系统错误,请联系工作人员');
        }
        $scope.showMessage();
    })
});
/**
 * Created by DELL on 2017/4/20.
 * 课程待使用页面列表
 */

app.controller('unusedCtrl', function ($scope, $rootScope, $http, dialogsManager, $ionicHistory) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }

    //获取待使用页面的数据
    var url = 'http://qa.aixingfu.net/v1/api-member/get-class-not-class?memberId=' + $rootScope.globalInfo.user.ids;
    //下拉刷新
    $scope.doRefresh = function () {
        $http.get(url)
            .success(function (newItems) {
                $scope.toUse = newItems;
            })
            .finally(function () {
                $scope.$broadcast('scroll.refreshComplete');
            });
    };
    //获取待使用课程数据
    $http({
        url: url,
        method: 'GET',
    }).success(function (date) {
        $scope.toUse = date;
    }).error(
        function (data) {
            console.log(data);
            $scope.showMessage = function () {
                dialogsManager.showMessage('系统错误,请联系工作人员');
            }
            $scope.showMessage();
        }
        );

    //点击待使用详情页面
    $scope.unusedDetail = function (id, aboutId, type) {
        $rootScope.id = id;
        $rootScope.aboutId = aboutId;
        if (type == 1) {
            $rootScope.type1 = "charge";
        }
        if (type == 2) {
            $rootScope.type1 = "group";
        }
        location.href = '#/tab/course/unusedDetail/:' + id;
    }
    $scope.wwww = function () {
        location.href = '#/tab/course';
    }
});

/**
 * Created by DELL on 2017/4/20.
 * 课程待使用详情页面
 */
app.controller('unusedDetailCtrl', function ($scope, $ionicHistory, $rootScope, $stateParams, $http, $state, $location, dialogsManager) {

    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    $scope.courseDetail = function () {
        $http({
            method: 'GET',
            url: 'http://qa.aixingfu.net/v1/api-member/get-class-detail?aboutId=' + $rootScope.aboutId + '&type=' + $rootScope.type1,
        }).success(function (data) {
            $scope.item = data;
            // if ($scope.item.courseFlag) {
            //     var courseNum = $scope.item.charge.packageClass;
            //     for (var i = 0; i < courseNum.length; i++) {
            //         if (parseInt(courseNum[i].status) == 1) {
            //             $scope.classState = 'ion-checkmark-circled';
            //             $scope.yetClass = true;
            //         } else if (parseInt(courseNum[i].status) == 2) {
            //             $scope.classState = '';
            //             $scope.yetClass = false;
            //         }
            //     }
            // }

            for (var key in $scope.item.packageClass) {
                if ($scope.item.packageClass.hasOwnProperty(key)) {
                    if ($scope.item.packageClass[key].status == 1) {
                        $scope.classState = 'ion-checkmark-circled';
                        $scope.yetClass = true;
                    }
                    if ($scope.item.packageClass[key].status == 2) {
                        $scope.classState = '';
                        $scope.yetClass = false;
                    }
                }
            }
        }).error(
            function (data) {
                console.log(data);
                $scope.showMessage = function () {
                    dialogsManager.showMessage('系统错误,请联系工作人员');
                }
                $scope.showMessage();
            }
            );
    }
    $scope.courseDetail();
    //取消预约页面
    $scope.cancelOrder = function (cancelOrderId, type) {
        if (type == true) {
            $scope.showMessage = function () {
                dialogsManager.showMessage("已经快要上课了，不能取消预约");
            }
            $scope.showMessage();
            return
        }
        //取消预约
        var url = 'http://qa.aixingfu.net/v1/api-about-record/cancel-about?aboutId=' + $rootScope.aboutId;
        $http({
            method: 'GET',
            url: url,
        }).success(function (data) {
            $scope.item = data;
            location.href = '#/tab/course/cancelOrder'
        }).error(
            function (data) {
                console.log(data);
                $scope.showMessage = function () {
                    dialogsManager.showMessage('系统错误,请联系工作人员');
                }
                $scope.showMessage();
            }
            );
    }
    //团课地图
    $scope.mapLeagueClick = function (id, date) {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/' + $rootScope.dates + '/leagueCourseMap';
    }
    //私教地图
    $scope.mapPrivateClick = function (id) {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/privateCourseMap';
    }
    //跳转团课详情
    $scope.leagueDetailClick = function (id, date) {
        $rootScope.idsw = id;
        $rootScope.dates = date;
        location.href = '#/tab/course/leagueCourseDetail/:' + id + '/' + date;
    }

    //跳转私课详情页面
    $scope.privateDetailClick = function (id) {
        $rootScope.idsw = id;
        location.href = '#/tab/course/privateCourseDetails/:' + id;
    }
});

/**
 * Created by DELL on 2017/4/20.
 * 课程待评价页面列表
 */


// app.controller('futureEvaluateCtrl', function ($scope, $rootScope, $stateParams, dialogsManager, $ionicHistory) {
//     //返回上一页
//     $scope.backPrePage = function () {
//         $ionicHistory.goBack();
//     }

//     //点击带评价详情页面
//     $scope.futureEvaluateDetail = function (id) {
//         $rootScope.idFe = id;
//         location.href = '#/tab/course/futureEvaluateDetail/:' + id;
//     }
// });

/**
 * Created by DELL on 2017/4/20.
 * 课程待评价详情页面
 */
// app.controller('futureEvaluateDetailCtrl', function ($scope, $rootScope, $stateParams, $ionicHistory) {
//     //返回上一页
//     $scope.backPrePage = function () {
//         $ionicHistory.goBack();
//     }
//     $scope.writeEvaluate = function () {
//         location.href = '#/tab/course/writeEvaluate'
//     }
// });

/**
 * Created by DELL on 2017/4/20.
 * 课程取消的页面列表
 */
app.controller('cancelCourseCtrl', function ($scope, $rootScope, $stateParams, $http, dialogsManager, $ionicHistory) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    var url = 'http://qa.aixingfu.net/v1/api-member/get-class-cancel-class?memberId=' + $rootScope.globalInfo.user.ids;
    //下拉刷新
    $scope.doRefresh = function () {
        $http.get(url)
            .success(function (newItems) {
                $scope.recentCourse = newItems;
            })
            .finally(function () {
                $scope.$broadcast('scroll.refreshComplete');
            });
    };
    //获取已取消课程数据
    $scope.allOrdersData = function () {
        $http({
            url: url,
            method: 'GET',
        }).success(function (date) {
            $scope.cancelCourses = date;
        }).error(
            function (data) {
                console.log(data);
                $scope.showMessage = function () {
                    dialogsManager.showMessage('系统错误,请联系工作人员');
                }
                $scope.showMessage();
            }
            );
    }
    $scope.allOrdersData();



    //已取消详情页面
    $scope.cancelCourseDetail = function (id, aboutId, type) {
        $rootScope.idCc = id;
        $rootScope.aboutId = aboutId;
        if (type == 1) {
            $rootScope.type1 = "charge";
        }
        if (type == 2) {
            $rootScope.type1 = "group";
        }
        location.href = '#/tab/course/cancelCourseDetail/:' + id;
    }
});
/*
* 课程*---面中团课课程详情
* */

app.controller('leagueCourseDetailCtrl', function ($scope, $rootScope, $stateParams, $http, $ionicHistory, $state, dialogsManager) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    var url = 'http://qa.aixingfu.net/v1/api-class/get-detail?id=' + $rootScope.idsw;
    $http({
        url: url,
        method: 'GET',
    }).success(function (date) {
        $scope.items = date;
        $rootScope.details = date;

        $rootScope.itemsCoadchId = $scope.items.coach_id;
        $rootScope.itemsVenueId = $scope.items.venue_id;
    }).error(
        function (data) {
            console.log(data);
            $scope.showMessage = function () {
                dialogsManager.showMessage('系统错误,请联系工作人员');
            }
            $scope.showMessage();
        }
        );
    /**
     * 场馆
     */
    $scope.venueDetailsClick = function (id, date) {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/' + $rootScope.dates + '/leagueCourseVenueDetail';
    }
    /**
     * 地图
     */
    $scope.venueMapClick = function (id, date) {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/' + $rootScope.dates + '/leagueCourseMap';
    }
    /**
     * 教练
     */
    $scope.coachDetailsClick = function () {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/' + $rootScope.dates + '/leagueCourseCoach';
    }

});

//私课课程详情
app.controller('privateCourseDetailsCtrl', function ($rootScope, $scope, $stateParams, $http, $ionicHistory, $state, dialogsManager) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    $http({
        url: 'http://qa.aixingfu.net/v1/api-private/get-detail?id=' + $rootScope.idsw,
        method: 'GET',
    }).success(function (date) {
        $scope.items = date;

        // 课程id
        $rootScope.classType = $scope.items.classId


    }).error(
        function (data) {
            console.log(data);
            $scope.showMessage = function () {
                dialogsManager.showMessage('系统错误,请联系工作人员');
            }
            $scope.showMessage();
        }
        );

    $rootScope.date = '4';
    // 跳转 产品课程
    $scope.productCourse = function () {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/' + $rootScope.date + '/privateProductCourse';
    }
    //地图
    $scope.mapClick = function () {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/privateCourseMap';
    }
});

/**
 * 课程页面产品课程
 */
app.controller('privateProductCourseCtrl', function ($rootScope, $scope, $stateParams, $http) {

    var url = 'http://qa.aixingfu.net/v1/api-private/get-class-package-detail?id=' + $rootScope.idsw;

    $http({
        url: url,
        method: 'GET',
    }).success(function (date) {
        $scope.items = date;
        for (var key in $scope.items.packageClass) {
            if ($scope.items.packageClass.hasOwnProperty(key)) {
                if ($scope.items.packageClass[key].status == 1) {
                    $scope.aaa = 'ion-checkmark-circled';
                }
                if ($scope.items.packageClass[key].status == 2) {
                    $scope.aaa = '';
                }
            }
        }
    }).error(
        function (data) {
            console.log(data);
            $scope.showMessage = function () {
                dialogsManager.showMessage('系统错误,请联系工作人员');
            }
            $scope.showMessage();
        }
        );

})

/**
 * Created by DELL on 2017/4/20.
 * 课程取消详情页面
 */

app.controller('cancelCourseDetailCtrl', function ($scope, $rootScope, $stateParams, $http, dialogsManager, $ionicHistory) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    //当前取消预约课程的数据
    $scope.courseDetail = function () {
        $http({
            method: 'GET',
            url: 'http://qa.aixingfu.net/v1/api-member/get-class-detail?aboutId=' + $rootScope.aboutId + '&type=' + $rootScope.type1,
        }).success(function (data) {
            $scope.cancelCourse = data;
            // if ($scope.cancelCourse.courseFlag) {
            //     var courseNum = $scope.cancelCourse.packageClass;
            //     for (var i = 0; i < courseNum.length; i++) {
            //         if (parseInt(courseNum[i].status) == 1) {
            //             $scope.classState = 'ion-checkmark-circled';
            //             $scope.yetClass = true;
            //         } else if (parseInt(courseNum[i].status) == 2) {
            //             $scope.classState = '';
            //             $scope.yetClass = false;
            //         }
            //     }
            // }
        }).error(
            function (data) {
                console.log(data);
                $scope.showMessage = function () {
                    dialogsManager.showMessage('系统错误,请联系工作人员');
                }
                $scope.showMessage();
            }
            );
    }
    $scope.courseDetail();

    //跳转团课详情
    $scope.leagueDetailClick = function (id, date) {
        $rootScope.idsw = id;
        $rootScope.dates = date;
        location.href = '#/tab/course/leagueCourseDetail/:' + id + '/' + date;
    }
    //跳转私课详情页面
    $scope.privateDetailClick = function (id) {
        $rootScope.idsw = id;
        location.href = '#/tab/course/privateCourseDetails/:' + id;
    }
    //地图
    $scope.mapLeagueClick = function () {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/' + $rootScope.dates + '/leagueCourseMap';
    }
    //地图
    $scope.mapPrivateClick = function () {
        location.href = '#/tab/course/:' + $rootScope.idsw + '/privateCourseMap';
    }
});

/**
 * Created by DELL on 2017/4/25.
 * 取消预约页面
 */

app.controller('cancelOrderCtrl', function ($scope, $rootScope, $stateParams, $state, dialogsManager, $ionicHistory) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    //点击跳转全部订单页面
    $scope.allOrders = function () {
        location.href = '#/tab/course/allOrders';
    }
    //点击课程首页
    $scope.course = function () {
        $state.go('tab.course');
    }
});


// 场馆详情
app.controller('leagueCourseVenueDetailCtrl', function ($scope, $rootScope, $stateParams, $http, $ionicHistory, dialogsManager) {
    var url = 'http://qa.aixingfu.net/v1/api-venue/get-venue-detail?id=' + $rootScope.itemsVenueId;
    $http({
        url: url,
        method: 'GET',
    }).success(function (date) {
        $scope.items = date;
    }).error(
        function (data) {
            console.log(data);
            $scope.showMessage = function () {
                dialogsManager.showMessage('系统错误,请联系工作人员');
            }
            $scope.showMessage();
        }
        );

    $scope.back = function () {
        $ionicHistory.goBack()
    }
})

// 教练详情
app.controller('leagueCourseCoachCtrl', function ($scope, $rootScope, $stateParams, $http, dialogsManager,$ionicHistory) {
    $scope.back = function(){
        $ionicHistory.goBack()
    }
    var url = 'http://qa.aixingfu.net/v1/api-coach/get-coach-detail?id=' + $rootScope.itemsCoadchId;
    $http({
        url: url,
        method: 'GET',
    }).success(function (date) {
        $scope.items = date;
    }).error(
        function (data) {
            console.log(data);
            $scope.showMessage = function () {
                dialogsManager.showMessage('系统错误,请联系工作人员');
            }
            $scope.showMessage();
        }
        );
})
/**
 * privateTeachMapCtrl 地图
 */
app.controller('privateCourseMapCtrl', function ($rootScope, $scope, $stateParams, $http, $ionicHistory) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    //创建和初始化地图函数：
    function initMap() {
        createMap();//创建地图
        setMapEvent();//设置地图事件
        addMapControl();//向地图添加控件
        addMarker();//向地图中添加marker
    }


    //创建地图函数：
    function createMap() {
        var map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图
        var point = new BMap.Point(113.676957, 34.763269);//定义一个中心点坐标
        map.centerAndZoom(point, 17);//设定地图的中心点和坐标并将地图显示在地图容器中
        window.map = map;//将map变量存储在全局
    }



    //地图事件设置函数：
    function setMapEvent() {
        map.disableDragging();//禁用地图拖拽事件
        map.disableScrollWheelZoom();//禁用地图滚轮放大缩小，默认禁用(可不写)
        map.disableDoubleClickZoom();//禁用鼠标双击放大
        map.disableKeyboard();//禁用键盘上下左右键移动地图，默认禁用(可不写)
    }

    //地图控件添加函数：
    function addMapControl() {
    }


    //标注点数组
    var markerArr = [{ title: "", content: "我爱运动健身馆大上海城店", point: "113.676957|34.763269", isOpen: 0, icon: { w: 25, h: 25, l: -2, t: 21, x: 6, lb: 5 } }
    ];
    //创建marker
    function addMarker() {
        for (var i = 0; i < markerArr.length; i++) {
            var json = markerArr[i];
            var p0 = json.point.split("|")[0];
            var p1 = json.point.split("|")[1];
            var point = new BMap.Point(p0, p1);
            var iconImg = createIcon(json.icon);
            var marker = new BMap.Marker(point, { icon: myIcon });
            var iw = createInfoWindow(i);
            var label = new BMap.Label(json.title, { "offset": new BMap.Size(json.icon.lb - json.icon.x + 10, -20) });
            marker.setLabel(label);
            map.addOverlay(marker);
            label.setStyle({
                borderColor: "#808080",
                color: "#333",
                cursor: "pointer"
            });

            (function () {
                var index = i;
                var _iw = createInfoWindow(i);
                var _marker = marker;
                _marker.addEventListener("click", function () {
                    this.openInfoWindow(_iw);
                });
                _iw.addEventListener("open", function () {
                    _marker.getLabel().hide();
                })
                _iw.addEventListener("close", function () {
                    _marker.getLabel().show();
                })
                label.addEventListener("click", function () {
                    _marker.openInfoWindow(_iw);
                })
                if (!!json.isOpen) {
                    label.hide();
                    _marker.openInfoWindow(_iw);
                }
            })()
        }
    }
    //创建marker
    function addMarker() {
        for (var i = 0; i < markerArr.length; i++) {
            var json = markerArr[i];
            var p0 = json.point.split("|")[0];
            var p1 = json.point.split("|")[1];
            var point = new BMap.Point(p0, p1);
            var iconImg = createIcon(json.icon);
            var marker = new BMap.Marker(point, { icon: iconImg });
            var iw = createInfoWindow(i);
            var label = new BMap.Label(json.title, { "offset": new BMap.Size(json.icon.lb - json.icon.x + 10, -20) });
            marker.setLabel(label);
            map.addOverlay(marker);
            label.setStyle({
                borderColor: "#808080",
                color: "#333",
                cursor: "pointer"
            });

            (function () {
                var index = i;
                var _iw = createInfoWindow(i);
                var _marker = marker;
                _marker.addEventListener("click", function () {
                    this.openInfoWindow(_iw);
                });
                _iw.addEventListener("open", function () {
                    _marker.getLabel().hide();
                })
                _iw.addEventListener("close", function () {
                    _marker.getLabel().show();
                })
                label.addEventListener("click", function () {
                    _marker.openInfoWindow(_iw);
                })
                if (!!json.isOpen) {
                    label.hide();
                    _marker.openInfoWindow(_iw);
                }
            })()
        }
    }
    //创建InfoWindow
    function createInfoWindow(i) {
        var json = markerArr[i];
        var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>" + json.content + "</div>");
        return iw;
    }
    //创建一个Icon
    function createIcon(json) {
        var icon = new BMap.Icon("http://map.baidu.com/image/us_mk_icon.png", new BMap.Size(json.w, json.h), { imageOffset: new BMap.Size(-json.l, -json.t), infoWindowOffset: new BMap.Size(json.lb + 5, 1), offset: new BMap.Size(json.x, json.h) })
        return icon;
    }

    initMap();//创建和初始化地图
})

// 地图
app.controller('leagueCourseMapCtrl', function ($ionicHistory, $scope) {
    //返回上一页
    $scope.backPrePage = function () {
        $ionicHistory.goBack();
    }
    //创建和初始化地图函数：
    function initMap() {
        createMap();//创建地图
        setMapEvent();//设置地图事件
        addMapControl();//向地图添加控件
        addMarker();//向地图中添加marker
    }


    //创建地图函数：
    function createMap() {
        var map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图
        var point = new BMap.Point(113.676957, 34.763269);//定义一个中心点坐标
        map.centerAndZoom(point, 17);//设定地图的中心点和坐标并将地图显示在地图容器中
        window.map = map;//将map变量存储在全局
    }



    //地图事件设置函数：
    function setMapEvent() {
        map.disableDragging();//禁用地图拖拽事件
        map.disableScrollWheelZoom();//禁用地图滚轮放大缩小，默认禁用(可不写)
        map.disableDoubleClickZoom();//禁用鼠标双击放大
        map.disableKeyboard();//禁用键盘上下左右键移动地图，默认禁用(可不写)
    }

    //地图控件添加函数：
    function addMapControl() {
    }


    //标注点数组
    var markerArr = [{ title: "", content: "我爱运动健身馆大上海城店", point: "113.676957|34.763269", isOpen: 0, icon: { w: 25, h: 25, l: -2, t: 21, x: 6, lb: 5 } }
    ];
    //创建marker
    function addMarker() {
        for (var i = 0; i < markerArr.length; i++) {
            var json = markerArr[i];
            var p0 = json.point.split("|")[0];
            var p1 = json.point.split("|")[1];
            var point = new BMap.Point(p0, p1);
            var iconImg = createIcon(json.icon);
            var marker = new BMap.Marker(point, { icon: myIcon });
            var iw = createInfoWindow(i);
            var label = new BMap.Label(json.title, { "offset": new BMap.Size(json.icon.lb - json.icon.x + 10, -20) });
            marker.setLabel(label);
            map.addOverlay(marker);
            label.setStyle({
                borderColor: "#808080",
                color: "#333",
                cursor: "pointer"
            });

            (function () {
                var index = i;
                var _iw = createInfoWindow(i);
                var _marker = marker;
                _marker.addEventListener("click", function () {
                    this.openInfoWindow(_iw);
                });
                _iw.addEventListener("open", function () {
                    _marker.getLabel().hide();
                })
                _iw.addEventListener("close", function () {
                    _marker.getLabel().show();
                })
                label.addEventListener("click", function () {
                    _marker.openInfoWindow(_iw);
                })
                if (!!json.isOpen) {
                    label.hide();
                    _marker.openInfoWindow(_iw);
                }
            })()
        }
    }
    //创建marker
    function addMarker() {
        for (var i = 0; i < markerArr.length; i++) {
            var json = markerArr[i];
            var p0 = json.point.split("|")[0];
            var p1 = json.point.split("|")[1];
            var point = new BMap.Point(p0, p1);
            var iconImg = createIcon(json.icon);
            var marker = new BMap.Marker(point, { icon: iconImg });
            var iw = createInfoWindow(i);
            var label = new BMap.Label(json.title, { "offset": new BMap.Size(json.icon.lb - json.icon.x + 10, -20) });
            marker.setLabel(label);
            map.addOverlay(marker);
            label.setStyle({
                borderColor: "#808080",
                color: "#333",
                cursor: "pointer"
            });

            (function () {
                var index = i;
                var _iw = createInfoWindow(i);
                var _marker = marker;
                _marker.addEventListener("click", function () {
                    this.openInfoWindow(_iw);
                });
                _iw.addEventListener("open", function () {
                    _marker.getLabel().hide();
                })
                _iw.addEventListener("close", function () {
                    _marker.getLabel().show();
                })
                label.addEventListener("click", function () {
                    _marker.openInfoWindow(_iw);
                })
                if (!!json.isOpen) {
                    label.hide();
                    _marker.openInfoWindow(_iw);
                }
            })()
        }
    }
    //创建InfoWindow
    function createInfoWindow(i) {
        var json = markerArr[i];
        var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>" + json.content + "</div>");
        return iw;
    }
    //创建一个Icon
    function createIcon(json) {
        var icon = new BMap.Icon("http://map.baidu.com/image/us_mk_icon.png", new BMap.Size(json.w, json.h), { imageOffset: new BMap.Size(-json.l, -json.t), infoWindowOffset: new BMap.Size(json.lb + 5, 1), offset: new BMap.Size(json.x, json.h) })
        return icon;
    }

    initMap();//创建和初始化地图

})




